<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('岗位管理')"/>
    <style>
        .layui-table-cell .layui-form-checkbox[lay-skin=primary]{top:4px;}
    </style>
</head>
<body>
<div class="layui-layout layui-layout-iframe">
    <div class="layui-fluid">
        <div class="layui-card">
            <!--search form-->
            <form class="layui-form layui-card-header layuiadmin-card-header-auto search-form" id="searchForm">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">岗位编码</label>
                        <div class="layui-input-inline">
                            <input class="layui-input" type="text" name="postCode" placeholder="" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">岗位名称</label>
                        <div class="layui-input-inline">
                            <input class="layui-input" type="text" name="postName" placeholder="" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">岗位状态</label>
                        <div class="layui-input-inline input-sm">
                            <select name="status" th:with="type=${@dict.getType('sys_normal_disable')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button  type="button" class="layui-btn layuiadmin-btn-list" data-type="reload" lay-submit lay-filter="form-search" id="form-search"><i class="layui-icon">&#xe615;</i>搜索</button>
                    </div>
                </div>
            </form>
            <!--table actcion-->
            <div class="layui-card-body">
                <script type="text/html" id="table_post_toolbar">
                    <div class="layui-btn-group" shiro:hasPermission="system:post:add">
                        <button class="layui-btn layui-btn-sm" lay-event="toolbar-post-add" shiro:hasPermission="system:post:add"><i class="layui-icon">&#xe654;</i>新增</button>
                        <button class="layui-btn layui-btn-sm" lay-event="toolbar-post-remove" shiro:hasPermission="system:post:remove"><i class="layui-icon">&#xe640;</i>删除</button>
                    </div>
                </script>
                <table id="table_post" lay-filter="table_post"></table>
                <script type="text/html" id="col_operation">
                    <a shiro:hasPermission="system:post:edit" href="javascript:;" lay-event="edit">编辑</a><span class="splitor">|</span>
                    <a shiro:hasPermission="system:post:remove" href="javascript:;" lay-event="del">删除</a>
                </script>
            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:post:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:post:remove')}]];
    var prefix = ctx + "system/post";
</script>
<script>
    //页面事件列表
    var active = {
        reload: function () {
            search(false);
        },
        remove: function (ids) {
            layui.common.ajaxRemove(prefix + "/remove", ids, function (res) {
                active['reload'].call(this);
            });
        }
    };
    layui.use(['common'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            form = layui.form,
            common = layui.common,
            table = layui.table;
        //查询表单
        form.on('submit(form-search)', function (data) {
            search(true);
        });
        //表格初始化
        table.render({
            elem: '#table_post'
            , method: 'post'
            , url: prefix + "/list" //数据接口
            , toolbar: '#table_post_toolbar'
            , defaultToolbar: ['filter', 'exports']
            , autoSort: false 	//关闭前端排序 使用后端排序
            , limit: getPageSize()
            , limits: getPageList()
            , text: getLoadErrorMsg()
            , page: true
            , cols: new Array([
                {type: 'checkbox', width: 40}
                , {field: 'postCode', width: 150, title: '岗位编码'}
                , {field: 'postName', title: '岗位名称'}
                , {field: 'postSort', sort: true, width: 100, title: '显示顺序'}
                , {
                    field: 'status', width: 80, title: '状态',
                    templet: function (item) {
                        if (item.status === "0") {
                            return "<span class=\"layui-btn layui-btn-xs layui-btn-radius\">正常</span>";
                        } else {
                            return "<span class=\"layui-btn layui-btn-xs layui-btn-danger layui-btn-radius\">停用</span>";
                        }
                    }
                }
                , {field: 'createTime', title: '创建时间', width: 160, sort: true}
                , {title: '操作', width: 100, toolbar: '#col_operation'}
            ])
        });
        //表格工具栏按钮事件
        table.on('toolbar(table_post)', function (obj) {
            switch (obj.event) {
                case 'toolbar-post-remove':
                    var checks = table.checkStatus('table_post');
                    var ids = common.joinArray(checks.data, "postId");
                    active['remove'].call(this, ids);
                    break;
                case 'toolbar-post-add':
                    showSaveDialog('新增岗位', prefix + '/add/','btn-add');
                    break;
            }
        });
        //表格事件
        table.on('tool(table_post)', function (obj) {
            switch (obj.event) {
                case 'del':
                    active['remove'].call(this, obj.data.postId);
                    break;
                case 'edit':
                    showSaveDialog('修改岗位', prefix + '/edit/' + obj.data.postId,'btn-edit');
                    break;
            }
        });
        //监听排序事件
        table.on('sort(table_post)', function (obj) {
            var field = serializeJson($('#searchForm'));
            field.orderByColumn = obj.field ;
            field.isAsc = obj.type;
            table.reload('table_post', {where: field,page:{curr: 1}});//执行重载
        });
    });

    //展示保存表单弹出层
    function showSaveDialog(title,url,formSubmitBtnId){
        layer.open({
            type: 2,
            title: title,
            shade: false,
            fixed: false,
            area: ['800px', '400px'],
            content: url,
            btn: ["保存", "取消"],
            yes: function (index, layero) {
                var submitID = formSubmitBtnId;//表单提交按钮id
                var submit = layero.find('iframe').contents().find('#' + submitID);//button obj
                layero.find('iframe')[0].contentWindow.successCallback = function () {
                    parent.layer.closeAll();
                    parent.layer.alert('保存成功');
                    parent.search(); //刷新表格数据
                };
                submit.trigger('click');
            }
            , cancel: function (index, layero) {
                layui.layer.close(index);
                return false;
            }
        });
    }

    //表格查询
    function search(isResetCurr) {
        var field = serializeJson($('#searchForm'));
        var sortParam = getSort('table_post');
        if (sortParam) {
            field.orderByColumn = sortParam.sortField;
            field.isAsc = sortParam.sortMethod;
        }
        //执行重载
        var options = {where: field};
        if (isResetCurr == true) {
            options.page = {curr: 1};
        }
        layui.table.reload('table_post', options);
    }
</script>
</body>
</html>